package com.kg.fiber.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.eventbus.Subscribe;
import com.ivan.toolkit.common.Events;
import com.kg.fiber.FiberviewApplication;
import com.kg.fiber.service.Warnings;

public class WarningManagerPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -40355795330743315L;
	
	private List<WarningPanel> panels = Lists.newLinkedList();
	
	private Map<String, WarningPanel> map = Maps.newHashMap();

	/**
	 * Create the panel.
	 */
	public WarningManagerPanel() {
		setBackground(Color.WHITE);
		setLayout(null);
		this.addComponentListener(new ComponentListener() {

			@Override
			public void componentResized(ComponentEvent e) {
				updateLayout();
			}

			@Override
			public void componentMoved(ComponentEvent e) {
			}

			@Override
			public void componentShown(ComponentEvent e) {
			}

			@Override
			public void componentHidden(ComponentEvent e) {
			}
			
		});
		
		intialize();
	}
	
	private void intialize() {
		
		Warnings warnings = FiberviewApplication.getSpring().getBean(Warnings.class);
		List<Warnings.Item> items = warnings.getWarnings();
		for(Warnings.Item it : items) {
			
			addWarningItem(it);
			
		}
		
		Events.register(this);
		
	}
	
	@Subscribe
	private void dispatchEvent(Events event) {
		
		if(event.keyEqualWith("WARNING_HAPPEN")) {
			
			Warnings.Item item = event.get(Warnings.Item.class);
			
			addWarningItem(item);
		}
		
		if(event.keyEqualWith("WARNING_CONFIRM")) {
			
			Warnings.Item item = event.get(Warnings.Item.class);
			
			removeWarningItem(item);
			
		}
		
		
	}
	
	private void addWarningItem(Warnings.Item it) {
		
		WarningPanel panel = new WarningPanel();
		panel.setWarning(it);
		
		map.put(it.getHost(), panel);
		panels.add(panel);
		
		this.add(panel);
		
		// maybe it make the ui happy
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				updateLayout();
				//WarningManagerPanel.this.invalidate();
				WarningManagerPanel.this.repaint();
			}
		});
	}
	
	private void removeWarningItem(Warnings.Item it) {
		
		WarningPanel panel = map.remove(it.getHost());
		if(panel != null) {
			panel.setVisible(false);
			panels.remove(panel);
			this.remove(panel);
		}
				
		// maybe it make the ui happy
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				updateLayout();
				WarningManagerPanel.this.repaint();
			}
		});
	}
	
	private void updateLayout() {
		
		List<WarningPanel> copy = Lists.newArrayList();
		copy.addAll(panels);
		
		int width = this.getWidth();
		//int height = this.getHeight();
		
		//int count = copy.size();
		int rowHeight = 64;
		
		int y = 0;
		for(WarningPanel pane : copy) {
			pane.setBounds(0, y, width, rowHeight);
			SwingUtilities.updateComponentTreeUI(pane);
			y += rowHeight;
			pane.setVisible(true);
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		
		super.paintComponent(g);
		
		if(panels.isEmpty()) {
			
			JLabel lbl = new JLabel("正在运行（当前没有告警）");
			
			lbl.setHorizontalAlignment(JLabel.CENTER);
			
			lbl.setFont(new Font("微软雅黑", Font.PLAIN, 64));
			
			SwingUtilities.paintComponent(g, lbl, this, this.getBounds());
			
		}
	}
	
	

}
